
import { _decorator, Component, Node, Sprite, tween, Vec3, WorldNode3DToLocalNodeUI } from 'cc';
import { AppControll, ConfigKey, EventName, tasteMainGun_config } from '../manager/AppControll';
import { DataHelper } from '../system/DataHelper';
import EvtManger from '../tools/EvtManger';
import { GameData } from '../system/GameData';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = usingAgainNode
 * DateTime = Thu Jun 01 2023 17:56:05 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = usingAgainNode.ts
 * FileBasenameNoExtension = usingAgainNode
 * URL = db://assets/script/fight/usingAgainNode.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('TasteGunIcon')
export class TasteGunIcon extends Component {
    private mEndPoint: Vec3 = null;
    private mNdEnd: Node = null;
    private mConfig: tasteMainGun_config = null;
    private mStart: boolean = false;
    private mCountDown: number = 0;
    private mCurTime: number = 0;
    private mNdProgress: Node = null;
    protected onLoad(): void {
        this.DoMove();
        this.node.on(Node.EventType.TOUCH_END, this.OnClick, this);
    }

    InitTasteGunIcon(endPoint: Vec3, node: Node, config: tasteMainGun_config) {
        this.mEndPoint = endPoint;
        this.mNdEnd = node;
        this.mConfig = config;
        this.mNdProgress = this.node.getChildByName("mission_reward_bg_yellow").getChildByName("progress");
        this.mCountDown = Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.tasteGunCountDown).value);
    }
    start() {
        // [3]
    }


    DoMove() {
        const dropAnimation = tween()
            .to(1, { position: this.mEndPoint })
            .call(() => {
                this.node.parent = this.mNdEnd;
                this.node.position = Vec3.ZERO;
                this.mStart = true;
                this.mNdProgress.active = true;
                if (GameData.playerData.tasteGuide) {
                    GameData.playerData.tasteGuide = false;
                    this.node.getChildByName("firseClick").active = true;
                } else {
                    this.node.getChildByName("firseClick").active = false;
                }
            });

        // 播放动画
        tween(this.node)
            .then(dropAnimation)
            .start();
    }

    protected update(dt: number): void {
        if (this.mStart) {
            this.mCurTime += dt;
            this.mNdProgress.getComponent(Sprite).fillRange = 1 - this.mCurTime / this.mCountDown;
            if (this.mCurTime / this.mCountDown >= 1) {
                this.node.destroy();
            }
        }
    }

    private OnClick() {
        AppControll.ShelvedTime = 0;
        let total = Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.tasteGunUsingCountDown).value)
        EvtManger.do(EventName.tasteGun, this.mConfig, total);
        this.node.destroy();
    }
}
